package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.jptest.bean.ClassReportCard;
import edu.jptest.util.GetDataBaseConnection;

public class ClassReportCardDAO {

	// 插入团体成绩单
	public boolean insertClassReportCard(ClassReportCard reportCard) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into class_reportcard(group_id,top_score,low_score,average_score,examine_time,teacher,e_p_id) values(?,?,?,?,?,?,?)";
			if (connection != null)
				pstm = connection.prepareStatement(sql);
			else {
				System.out.println("连接为空值");
			}
			pstm.setInt(1, reportCard.getGroupId());
			pstm.setFloat(2, reportCard.getTopScore());
			pstm.setFloat(3, reportCard.getLowScore());
			pstm.setFloat(4, reportCard.getAverageScore());
			pstm.setDate(5, reportCard.getExamineTime());
			pstm.setString(6, reportCard.getTeacher());
			pstm.setInt(7, reportCard.getExaminePaperId());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 删除团体成绩单
	public boolean deleteClassReportCard(int reportCard_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from class_reportcard where c_r_id=?";
			if (connection != null)
				pstm = connection.prepareStatement(sql);
			else {
				System.out.println("连接为空值");
			}
			pstm.setInt(1, reportCard_id);
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 更该团体成绩单
	public boolean updateClassReportCard(ClassReportCard reportCard) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update class_reportcard set group_id=?,top_score=?,low_score=?,average_score=?,examine_time=?,teacher=?,e_p_id=?where c_r_id=?";
			if (connection != null)
				pstm = connection.prepareStatement(sql);
			else {
				System.out.println("连接为空值");
			}
			pstm.setInt(1, reportCard.getGroupId());
			pstm.setFloat(2, reportCard.getTopScore());
			pstm.setFloat(3, reportCard.getLowScore());
			pstm.setFloat(4, reportCard.getAverageScore());
			pstm.setDate(5, reportCard.getExamineTime());
			pstm.setString(6, reportCard.getTeacher());
			pstm.setInt(7, reportCard.getExaminePaperId());
			pstm.setInt(8, reportCard.getClassReportCardId());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 通过成绩单id，查询成绩单
	public ClassReportCard getClassReportCardById(int reportCard_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		ClassReportCard classReportCard = null;
		try {
			String sql = "select * from class_reportcard where c_r_id=?";
			if (connection != null)
				pstm = connection.prepareStatement(sql);
			else {
				System.out.println("连接为空值");
			}
			pstm.setInt(1, reportCard_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				classReportCard.setGroupId(resultSet.getInt(1));
				classReportCard.setTopScore(resultSet.getFloat(2));
				classReportCard.setLowScore(resultSet.getFloat(3));
				classReportCard.setAverageScore(resultSet.getFloat(4));
				classReportCard.setExamineTime(resultSet.getDate(5));
				classReportCard.setTeacher(resultSet.getString(6));
				classReportCard.setExaminePaperId(resultSet.getInt(7));
			}
			return classReportCard;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过团体id，查询成绩单id
	public List<Integer> getClassReportCardIdByGroupId(int group_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> classReportCardsId = new ArrayList<Integer>();
		try {
			String sql = "select c_r_id from class_reportcard where group_id=?";
			if (connection != null)
				pstm = connection.prepareStatement(sql);
			else {
				System.out.println("连接为空值");
			}
			pstm.setInt(1, group_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				classReportCardsId.add(resultSet.getInt(1));
			}
			return classReportCardsId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过某次考试试卷id，查询成绩单id
	public List<Integer> getClassReportCardIdByExaminId(int examin_paper_Id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		List<Integer> classReportCardsId = new ArrayList<Integer>();
		try {
			String sql = "select c_r_id from class_reportcard where e_p_id=?";
			if (connection != null)
				pstm = connection.prepareStatement(sql);
			else {
				System.out.println("连接为空值");
			}
			pstm.setInt(1, examin_paper_Id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				classReportCardsId.add(resultSet.getInt(1));
			}
			return classReportCardsId;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return null;
	}

	// 通过考试时间，查询成绩单id
	public int getClassReportIdByTime(java.sql.Date examin_time) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select c_r_id from class_reportcard where examine_time=?";
			if (connection != null)
				pstm = connection.prepareStatement(sql);
			else {
				System.out.println("连接为空值");
			}
			pstm.setDate(1, examin_time);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

	// 通过试卷编号、团体编号获得成绩单id
	public int getClassReportIdCardByGroupIdAndEPId(int group_id, int examin_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select c_r_id from class_reportcard where group_id=? and e_p_id=?";
			if (connection != null)
				pstm = connection.prepareStatement(sql);
			else {
				System.out.println("连接为空值");
			}
			pstm.setInt(1, group_id);
			pstm.setInt(1, examin_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				return resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;

	}

	// 团体成绩单中最高分，最低分，各分数段分析情况见考务DAO
}
